/**
 * \addtogroup httpd
 * @{
 */

/**
 * \file
 *         Web server script interface
 * \author
 *         Adam Dunkels <adam@sics.se>
 *
 */

/*
 * Copyright (c) 2001-2006, Adam Dunkels.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote
 *    products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This file is part of the uIP TCP/IP stack.
 *
 * $Id: httpd-cgi.c,v 1.2 2006/06/11 21:46:37 adam Exp $
 *
 * C:\ARMCode\FreeRTOS\Demo\CORTEX_LPC1768_GCC_Rowley\webserver\httpd-fs
 *
 *
 */

#include "uip.h"
#include "psock.h"
#include "httpd.h"
#include "httpd-cgi.h"
#include "httpd-fs.h"

#include <stdio.h>
#include <string.h>

 
HTTPD_CGI_CALL(status, "status", status_d );
HTTPD_CGI_CALL(speed, "speed", speed_d );
//HTTPD_CGI_CALL(command, "command", command_d );
//HTTPD_CGI_CALL(debug, "debug", debug_d );

extern Comms_Msg_Struct *webCommMsg;

static const struct httpd_cgi_call *calls[] = { &status, &speed, NULL };
//static const struct httpd_cgi_call *calls[] = { &status, &speed, &command, &debug, NULL };

/*---------------------------------------------------------------------------*/
static
PT_THREAD(nullfunction(struct httpd_state *s, char *ptr))
{
  PSOCK_BEGIN(&s->sout);
  ( void ) ptr;
  PSOCK_END(&s->sout);
}
/*---------------------------------------------------------------------------*/
httpd_cgifunction
httpd_cgi(char *name)
{
  const struct httpd_cgi_call **f;

  /* Find the matching name in the table, return the function. */
  for(f = calls; *f != NULL; ++f) {
    if(strncmp((*f)->name, name, strlen((*f)->name)) == 0) {
      return (*f)->function;
    }
  }
  return nullfunction;
}
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
extern unsigned int checkStart;

static unsigned short
generate_status(void *arg)
{	 
	static char statusMsgBuf[ 52 ];
	if(checkStart == 1)
	{	
		if (webCommMsg->msgType == DataManager)
		{
			sprintf ( statusMsgBuf, "Rover Status:<font color=\"red\"> ERROR</font></br>" );
		}
		else
		{
			sprintf ( statusMsgBuf, "Rover Status:<font color=\"green\"> Running</font></br>" );
		}
	}
	else
	{
		sprintf ( statusMsgBuf, "Rover Status:<font color=\"red\"> Stopped</font></br>" );	
	}
	strcat( uip_appdata, statusMsgBuf );
	return strlen( uip_appdata );
}

/*---------------------------------------------------------------------------*/

static
PT_THREAD(status_d(struct httpd_state *s, char *ptr))
{
  PSOCK_BEGIN(&s->sout);
  PSOCK_GENERATOR_SEND(&s->sout, generate_status, NULL);
  PSOCK_END(&s->sout);
}
/*---------------------------------------------------------------------------*/

static char speedMsgBuf[256];
static unsigned int speedRead;
static unsigned char* spdLimit;
static unsigned int speedLimit = 1;
static unsigned int optimalTime;


static unsigned short
generate_speed(void *arg)
{	 
	if ( webCommMsg->msgType == Speed )
	{
		speedRead = webCommMsg->data[1];
		switch(webCommMsg->data[0])
		{	
			case Low:
				spdLimit = "color=\"orange\"> Low";
				speedLimit = 999;
				break;
			case High:
				spdLimit = "color=\"yellow\"> High";
				speedLimit = 999;
				break;
			case Finish:
				spdLimit = "color=\"blue\"> Finish Line";
				speedLimit = 0;
				break;
			case None:
				if(speedLimit == 0)
				{
					spdLimit = "";
					speedLimit = 999;	
				}
				break;
			default:
				break;
		}
		
		sprintf ( speedMsgBuf, "Speed Zone:<font %s</font></br>", spdLimit );
		sprintf ( speedMsgBuf + strlen(speedMsgBuf), "Rover Speed: %d cm/s</br></br>", speedRead );
		if(speedLimit > 0)
		{
			if (webCommMsg->data[1] > speedLimit)
			{
				sprintf ( speedMsgBuf + strlen(speedMsgBuf), "<font color=\"Red\">Speed Zone Violated</font></br></br>" );
			}
			else if (webCommMsg->data[1] <= speedLimit)
			{
				sprintf ( speedMsgBuf + strlen(speedMsgBuf), "<font color=\"Green\">Within Speed Limit</font></br></br>" );
			}
		}

			int webDistance;
			webDistance = webCommMsg->data[3];
			webDistance = (webDistance<<8) + webCommMsg->data[4];
			int topSpeed = webCommMsg->data[2];
			
			optimalTime = webDistance/topSpeed;
			sprintf ( speedMsgBuf + strlen(speedMsgBuf), "Optimal Completion Time: %d s</br> Top Speed: %d cm/s</br> Total Distance: %d cm", optimalTime, topSpeed, webDistance );			
	}
	strcat( uip_appdata, speedMsgBuf );
	return strlen( uip_appdata );
}

/*---------------------------------------------------------------------------*/

static
PT_THREAD(speed_d(struct httpd_state *s, char *ptr))
{
  PSOCK_BEGIN(&s->sout);
  PSOCK_GENERATOR_SEND(&s->sout, generate_speed, NULL);
  PSOCK_END(&s->sout);
}
/*---------------------------------------------------------------------------*/
//
//static char cmdMsgBuf[32];
//int cmdCount = 1;
//unsigned char* curCmd;
//
//static unsigned short
//generate_command(void *arg)
//{	 
//	if (webCommMsg->msgType == Command)
//	{
//		switch( webCommMsg->data[0] )
//		{
//			case cmdTurnLeft:
//				curCmd = "Left";
//				break;
//			case cmdStraight:
//				curCmd = "Straight";
//				break;
//			case cmdTurnRight:
//				curCmd = "Right";
//				break;
//			case cmdStop:
//				curCmd = "Stop";
//				break;
//			default:
//				break;
//		}
//		sprintf ( cmdMsgBuf, "Current Command: %s", curCmd );
//	}
//
//	strcat( uip_appdata, cmdMsgBuf );
//	return strlen( uip_appdata );
//
//}
//
///*---------------------------------------------------------------------------*/
//
//static
//PT_THREAD(command_d(struct httpd_state *s, char *ptr))
//{
//  PSOCK_BEGIN(&s->sout);
//  PSOCK_GENERATOR_SEND(&s->sout, generate_command, NULL);
//  PSOCK_END(&s->sout);
//}
/*---------------------------------------------------------------------------*/

//static char debugBuff[80];
//
//static unsigned short
//generate_debug(void *arg)
//{	 
//	if (webCommMsg->msgType == Collision)
//	{
//		sprintf ( debugBuff, "Debug: L45:<b>%d</b> L90: <b>%d</b> R90: <b>%d</b> R45: <b>%d</b>", webCommMsg->data[0], webCommMsg->data[1], webCommMsg->data[2], webCommMsg->data[3] );	
//	}
//
//	strcat( uip_appdata, debugBuff );				  
//	return strlen( uip_appdata );
//}
//
///*---------------------------------------------------------------------------*/
//
//static
//PT_THREAD(debug_d(struct httpd_state *s, char *ptr))
//{
//  PSOCK_BEGIN(&s->sout);
//  PSOCK_GENERATOR_SEND(&s->sout, generate_debug, NULL);
//  PSOCK_END(&s->sout);
//}
/*---------------------------------------------------------------------------*/


/** @} */






